package com.group11.museum.backend.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
public class JsonToMySQL {

    private final JdbcTemplate jdbcTemplate;


    public JsonToMySQL(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @RequestMapping( value = "cityid", method = RequestMethod.GET)
    public String toMySQL(@RequestParam(value = "cityname") String cityname){

        // 创建返回json对象
        JSONObject res = new JSONObject();

        // 创建返回代码
        JSONObject returnCode = new JSONObject();
        returnCode.put("comment", "success");
        returnCode.put("code", "200");
        res.put("return", returnCode);
        try{
            String querySQL = String.format("select areaid from city_id where countyname='%s'", cityname);
            Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(querySQL);
            res.put("city_id", stringObjectMap.get("areaid"));
        }catch (Exception e){
            returnCode.put("code", "310");
            returnCode.put("comment", "query error or no row match condition");
        }
        return res.toJSONString();
    }

}
